package com.tengyun.middleplatform.trade.etl.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * HikariDataSource配置
 *
 * @author konkera
 * @date 2021/3/25
 */
@Configuration
public class HikariDataSourceConfig {
    @Value("${spring.datasource.hikari.auto-commit}")
    private boolean autoCommit;
    @Value("${spring.datasource.hikari.connectionTimeout}")
    private int connectionTimeout;
    @Value("${spring.datasource.hikari.idle-timeout}")
    private int idleTimeout;
    @Value("${spring.datasource.hikari.maxLifetime}")
    private int maxLifetime;
    @Value("${spring.datasource.hikari.minimum-idle}")
    private int minimumIdle;
    @Value("${spring.datasource.hikari.maximum-pool-size}")
    private int maximumPoolSize;

    /**
     * HikariDataSource初始化
     *
     * @param dataSource {@link HikariDataSource}
     */
    public void dataSource(HikariDataSource dataSource) {
        //自动提交行为设置
        dataSource.setAutoCommit(autoCommit);
        //连接超时时间设置
        dataSource.setConnectionTimeout(connectionTimeout);
        //连接空闲生命周期设置
        dataSource.setIdleTimeout(idleTimeout);
        //检查空余连接优化连接池设置时间,单位毫秒
        dataSource.setMaxLifetime(maxLifetime);
        //连接池保持最小空余连接数量
        dataSource.setMinimumIdle(minimumIdle);
        //连接池允许的最大连接数量
        dataSource.setMaximumPoolSize(maximumPoolSize);
    }
}
